package com.rfsp.mcp.init;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.rfsp.common.cache.DictCache;
import com.rfsp.common.pojo.vo.DictVO;
import com.rfsp.common.pojo.po.DictPO;
import com.rfsp.mcp.enums.McpConst.*;
import com.rfsp.mcp.enums.McpDictEnum;
import com.rfsp.common.service.DictService;

@Component
public class InitMcpDict implements ApplicationRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(InitMcpDict.class);

    @Autowired
    ServletContext application;

	@Autowired
	DictService dictService;
	
	@Autowired
    DictCache dictCache;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            ArrayList<DictVO> VersionTypeList = new ArrayList<>();
            LinkedHashMap<String, String> VersionTypeMap = new LinkedHashMap<>();
            for (VersionType type : VersionType.values()) {
                VersionTypeList.add(new DictVO(type.getCode(), type.getMsg()));
                VersionTypeMap.put(type.getCode(), type.getMsg());
            }

            ArrayList<DictVO> AuthStatusList = new ArrayList<>();
            LinkedHashMap<String, String> AuthStatusMap = new LinkedHashMap<>();
            for (AuthStatus status : AuthStatus.values()) {
                AuthStatusList.add(new DictVO(status.getCode(), status.getMsg()));
                AuthStatusMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> ProductStatusList = new ArrayList<>();
            LinkedHashMap<String, String> ProductStatusMap = new LinkedHashMap<>();
            for (ProductStatus status : ProductStatus.values()) {
                ProductStatusList.add(new DictVO(status.getCode(), status.getMsg()));
                ProductStatusMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> RepayTypeList = new ArrayList<>();
            LinkedHashMap<String, String> RepayTypeMap = new LinkedHashMap<>();
            for (RepayType status : RepayType.values()) {
                RepayTypeList.add(new DictVO(status.getCode(), status.getMsg()));
                RepayTypeMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> AreaTypeList = new ArrayList<>();
            LinkedHashMap<String, String> AreaTypeMap = new LinkedHashMap<>();
            for (AreaType status : AreaType.values()) {
                AreaTypeList.add(new DictVO(status.getCode(), status.getMsg()));
                AreaTypeMap.put(status.getCode(), status.getMsg());
            }


            ArrayList<DictVO> MiTypeList = new ArrayList<>();
            LinkedHashMap<String, String> MiTypeMap = new LinkedHashMap<>();
            for (MiType status : MiType.values()) {
                MiTypeList.add(new DictVO(status.getCode(), status.getMsg()));
                MiTypeMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> BussStatusList = new ArrayList<>();
            LinkedHashMap<String, String> BussStatusMap = new LinkedHashMap<>();
            for (BusinessStatus status : BusinessStatus.values()) {
                BussStatusList.add(new DictVO(status.getCode(), status.getMsg()));
                BussStatusMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> BussTypeList = new ArrayList<>();
            LinkedHashMap<String, String> BussTypeMap = new LinkedHashMap<>();
            for (BusinessType status : BusinessType.values()) {
                BussTypeList.add(new DictVO(status.getCode(), status.getMsg()));
                BussTypeMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> PHYWTypeList = new ArrayList<>();
            LinkedHashMap<String, String> PHYWTypeMap = new LinkedHashMap<>();
            for (PHYWType status : PHYWType.values()) {
                PHYWTypeList.add(new DictVO(status.getCode(), status.getMsg()));
                PHYWTypeMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> YLLBList = new ArrayList<>();
            LinkedHashMap<String, String> YLLBMap = new LinkedHashMap<>();
            for (YLLB status : YLLB.values()) {
                YLLBList.add(new DictVO(status.getCode(), status.getMsg()));
                YLLBMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> XZLXList = new ArrayList<>();
            LinkedHashMap<String, String> XZLXMap = new LinkedHashMap<>();
            for (XZLX status : XZLX.values()) {
                XZLXList.add(new DictVO(status.getCode(), status.getMsg()));
                XZLXMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> FinanceStatusList = new ArrayList<>();
            LinkedHashMap<String, String> FinanceStatusMap = new LinkedHashMap<>();
            for (FinanceStatus status : FinanceStatus.values()) {
                FinanceStatusList.add(new DictVO(status.getCode(), status.getMsg()));
                FinanceStatusMap.put(status.getCode(), status.getMsg());
            }


            ArrayList<DictVO> CurrencyList = new ArrayList<>();
            LinkedHashMap<String, String> CurrencyMap = new LinkedHashMap<>();
            List<DictPO> queryCurrencyList = dictService.queryList("DD_106");
            for (DictPO po : queryCurrencyList) {
                CurrencyList.add(new DictVO(po.getDiccode(), po.getDicname()));
                CurrencyMap.put(po.getDiccode(), po.getDicname());
            }

            ArrayList<DictVO> loanLimitList = new ArrayList<>();
            LinkedHashMap<String, String> loanLimitMap = new LinkedHashMap<>();
            List<DictPO> loanLimitDictList = dictService.queryList("DD_305");
            for (DictPO po : loanLimitDictList) {
                loanLimitList.add(new DictVO(po.getDiccode(), po.getDicname()));
                loanLimitMap.put(po.getDiccode(), po.getDicname());
            }

            ArrayList<DictVO> orgTypeList = new ArrayList<>();
            LinkedHashMap<String, String> orgTypeMap = new LinkedHashMap<>();
            List<DictPO> orgTypeDicList = dictService.queryList("DD_112");
            for (DictPO po : orgTypeDicList) {
                orgTypeList.add(new DictVO(po.getDiccode(), po.getDicname()));
                orgTypeMap.put(po.getDiccode(), po.getDicname());
            }

            ArrayList<DictVO> FinanceAuthStatusList = new ArrayList<>();
            LinkedHashMap<String, String> FinanceAuthStatusMap = new LinkedHashMap<>();
            for (AuthStatus status : AuthStatus.values()) {
                FinanceAuthStatusList.add(new DictVO(status.getCode(), status.getMsg()));
                FinanceAuthStatusMap.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> AuthStatus123List = new ArrayList<>();
            LinkedHashMap<String, String> AuthStatus123Map = new LinkedHashMap<>();
            for (AuthStatus123 status : AuthStatus123.values()) {
                AuthStatus123List.add(new DictVO(status.getCode(), status.getMsg()));
                AuthStatus123Map.put(status.getCode(), status.getMsg());
            }

            ArrayList<DictVO> FinanceLimitList = new ArrayList<>();
            LinkedHashMap<String, String> FinanceLimitMap = new LinkedHashMap<>();
            for (FinanceLimit financeLimit : FinanceLimit.values()) {
                FinanceLimitList.add(new DictVO(financeLimit.getCode(), financeLimit.getMsg()));
                FinanceLimitMap.put(financeLimit.getCode(), financeLimit.getMsg());
            }


            dictCache.DictMap.put(McpDictEnum.VersionType.name(), VersionTypeMap);
            dictCache.DictMap.put(McpDictEnum.AuthStatus.name(), AuthStatusMap);
            dictCache.DictMap.put(McpDictEnum.AuthStatus123.name(), AuthStatus123Map);
            dictCache.DictMap.put(McpDictEnum.ProductStatus.name(), ProductStatusMap);
            dictCache.DictMap.put(McpDictEnum.RepayType.name(), RepayTypeMap);
            dictCache.DictMap.put(McpDictEnum.AreaType.name(), AreaTypeMap);
            dictCache.DictMap.put(McpDictEnum.MiType.name(), MiTypeMap);
            dictCache.DictMap.put(McpDictEnum.BussStatus.name(), BussStatusMap);
            dictCache.DictMap.put(McpDictEnum.BussType.name(), BussTypeMap);
            dictCache.DictMap.put(McpDictEnum.PHYWType.name(), PHYWTypeMap);
            dictCache.DictMap.put(McpDictEnum.YLLB.name(), YLLBMap);
            dictCache.DictMap.put(McpDictEnum.XZLX.name(), XZLXMap);
            dictCache.DictMap.put(McpDictEnum.FinanceStatus.name(), FinanceStatusMap);
            dictCache.DictMap.put(McpDictEnum.FinanceLimit.name(), FinanceLimitMap);

            //字典表
            dictCache.DictMap.put(McpDictEnum.CurrencyType.name(), CurrencyMap);
            dictCache.DictMap.put(McpDictEnum.TimeLimitType.name(), loanLimitMap);
            dictCache.DictMap.put(McpDictEnum.OrgType.name(), orgTypeMap);

            dictCache.DictList.put(McpDictEnum.VersionTypeList.name(), VersionTypeList);
            dictCache.DictList.put(McpDictEnum.AuthStatusList.name(), AuthStatusList);
            dictCache.DictList.put(McpDictEnum.AuthStatus123List.name(), AuthStatus123List);
            dictCache.DictList.put(McpDictEnum.ProductStatusList.name(), ProductStatusList);
            dictCache.DictList.put(McpDictEnum.RepayTypeList.name(), RepayTypeList);
            dictCache.DictList.put(McpDictEnum.AreaTypeList.name(), AreaTypeList);
            dictCache.DictList.put(McpDictEnum.MiTypeList.name(), MiTypeList);
            dictCache.DictList.put(McpDictEnum.BussStatusList.name(), BussStatusList);
            dictCache.DictList.put(McpDictEnum.BussTypeList.name(), BussTypeList);
            dictCache.DictList.put(McpDictEnum.PHYWTypeList.name(), PHYWTypeList);
            dictCache.DictList.put(McpDictEnum.YLLBList.name(), YLLBList);
            dictCache.DictList.put(McpDictEnum.XZLXList.name(), XZLXList);
            dictCache.DictList.put(McpDictEnum.FinanceStatusList.name(), FinanceStatusList);
            dictCache.DictList.put(McpDictEnum.FinanceLimitList.name(), FinanceLimitList);

            //字典表
            dictCache.DictList.put(McpDictEnum.CurrencyList.name(), CurrencyList);
            dictCache.DictList.put(McpDictEnum.TimeLimitList.name(), loanLimitList);
            dictCache.DictList.put(McpDictEnum.OrgTypeList.name(), orgTypeList);

        } catch (Exception e) {
            logger.error("init PrivTree cache error!", e);
        }


    }

}
